package com.example.neusoft215gdh.androidpro_21.Activity;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.TextView;
import android.widget.Toast;

import com.example.neusoft215gdh.androidpro_21.Adapter.MasterQueAdapter;
import com.example.neusoft215gdh.androidpro_21.JavaBean.MasterQue;
import com.example.neusoft215gdh.androidpro_21.JavaBean.master;
import com.example.neusoft215gdh.androidpro_21.Listener.MasterDetailListener;
import com.example.neusoft215gdh.androidpro_21.Listener.MasterQueListener;
import com.example.neusoft215gdh.androidpro_21.Model.MasterModel;
import com.example.neusoft215gdh.androidpro_21.R;
import com.squareup.picasso.Picasso;

import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class MasterdetailActivity extends AppCompatActivity implements View.OnClickListener,MasterDetailListener,MasterQueListener{

    TextView masterName;
    TextView masterIntro;
    TextView masterLocal;
    RadioButton master_Course1;
    RadioButton master_Que1;
    Intent intent;
    ImageView ic_reg;
    String Pid;
    RecyclerView recyclerView;
    ImageView masterPic;
    ImageButton m_collect;
    int userid;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_masterdetail);
        initView();
        intent=getIntent();
         Pid=intent.getStringExtra("id");
        MasterModel ml=new MasterModel();
        ml.getDetailMaster(Pid,this);
        ic_reg.setOnClickListener(this);
        master_Que1.setOnClickListener(this);
        m_collect.setOnClickListener(this);
        isCollect();
    }

    public void initView(){
        masterName= (TextView) findViewById(R.id.master_name);
        masterLocal= (TextView) findViewById(R.id.master_local);
        master_Course1= (RadioButton) findViewById(R.id.master_Course1);
        masterIntro= (TextView) findViewById(R.id.master_intro);
        master_Que1= (RadioButton) findViewById(R.id.master_Que1);
        masterPic= (ImageView) findViewById(R.id.masterPic);
        ic_reg= (ImageView) findViewById(R.id.reg_return);
        m_collect= (ImageButton) findViewById(R.id.love);
        recyclerView= (RecyclerView) findViewById(R.id.recQue);
        recyclerView.setLayoutManager(new LinearLayoutManager(this.getApplication()));
        recyclerView.setItemAnimator(new DefaultItemAnimator());
    }

    @Override
    public void onClick(View view) {
     switch (view.getId()){
         case R.id.reg_return:
             finish();
             break;
         case R.id.master_Que1:
             MasterModel mt=new MasterModel();
             mt.getMasterQue(Pid,this);
             break;
         case R.id.love:
             collect();
             break;
     }
    }

    @Override
    public void onResponse(master bean) {

        masterName.setText(bean.getRealname());
        masterIntro.setText(bean.getIntroduction());
        masterLocal.setText(bean.getProvince_name());
        //masterPic.set
        if(!bean.getThumb().isEmpty())
        {
            Picasso.with(this).load("http://172.24.10.175/workout/Uploads/"+bean.getThumb()).into(masterPic);
        }
        else
        {
            masterPic.setImageResource(R.drawable.ic_person);
        }
    }

    @Override
    public void onResponse(List<MasterQue> bean) {
         recyclerView.setAdapter(new MasterQueAdapter(this,bean));
    }

    @Override
    public void onFail(String msg) {

    }

    private void isCollect(){
        SharedPreferences user=getSharedPreferences("Userid",0);
        userid=Integer.valueOf(user.getString("user_id","")).intValue();
        MasterModel masterModel = new MasterModel();
        Call<Integer> call = masterModel.isCollected(Pid,userid);
        call.enqueue(new Callback<Integer>() {
            @Override
            public void onResponse(Call<Integer> call, Response<Integer> response) {
                if (response.body()==0){
                    m_collect.setImageResource(R.drawable.ic_love_press);
                }else{
                    m_collect.setImageResource(R.drawable.ic_love);
                }
            }
            @Override
            public void onFailure(Call<Integer> call, Throwable t) {
            }
        });
    }

    private void collect() {
        SharedPreferences user=getSharedPreferences("Userid",0);
        userid=Integer.valueOf(user.getString("user_id","")).intValue();
        MasterModel masterModel = new MasterModel();
        Call<Integer> call = masterModel.userCollectMaster(userid,Pid);
        call.enqueue(new Callback<Integer>() {

            @Override
            public void onResponse(Call<Integer> call, Response<Integer> response) {
                if(response.body()!=0){
                    m_collect.setImageResource(R.drawable.ic_love_press);
                    Toast.makeText(com.example.neusoft215gdh.androidpro_21.Activity.MasterdetailActivity.this,"收藏成功",Toast.LENGTH_LONG).show();
                }else {
                    m_collect.setImageResource(R.drawable.ic_love);
                    Toast.makeText(com.example.neusoft215gdh.androidpro_21.Activity.MasterdetailActivity.this,"取消成功！",Toast.LENGTH_LONG).show();
                }
            }

            @Override
            public void onFailure(Call<Integer> call, Throwable t) {

            }
        });
    }
}
